package com.atstudying.mall.ware.service.impl;

import com.atstudying.common.utils.PageUtils;
import com.atstudying.common.utils.Query;
import com.atstudying.mall.ware.dao.PurchaseDetailDao;
import com.atstudying.mall.ware.entity.PurchaseDetailEntity;
import com.atstudying.mall.ware.service.PurchaseDetailService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                new QueryWrapper<PurchaseDetailEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * {
     page: 1,//当前页码
     limit: 10,//每页记录数
     sidx: 'id',//排序字段
     order: 'asc/desc',//排序方式
     key: '华为',//检索关键字
     status: 0,//状态
     wareId: 1,//仓库id
     }
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<PurchaseDetailEntity> wrapper = new QueryWrapper<>();

        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)){

            //和其他条件进行整体拼接
            wrapper.and(w->{
                w.eq("id",key).or().eq("sku_id",key);

            });
        }

        String status = (String) params.get("status");
        if (!StringUtils.isEmpty(status)){

            wrapper.eq("status",status);
        }

        String wareId = (String) params.get("wareId");
        if (!StringUtils.isEmpty(wareId)){

            wrapper.eq("ware_id",key);
        }
        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);

    }

    @Override
    public List<PurchaseDetailEntity> listDetailByPurchaseId(Long id) {

        //按照条件查询
        this.list(new QueryWrapper<PurchaseDetailEntity>().eq("purchase_id",id));
        return null;
    }

}